package common.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import common.interf.IMyDateHandle;

public class MyDateHandleImpl implements IMyDateHandle {
	/******************** date response **********************/
	/**
	 * 日期转字符串
	 * 
	 * @param d
	 *            : 日期
	 * @return 成功 转换后串 失败 null
	 */
	public final String MyDateToString(Date d) {
		if (null == d)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		String rets = null;
		try {
			rets = simpleDateFormat.format(d);
		} catch (Exception e) {
		}
		return rets;
	}

	/**
	 * 日期时间转字符串
	 * 
	 * @param d
	 *            : 日期
	 * @return 成功 转换后串 失败 null
	 */
	public final String MyDateTimeToString(Date d) {
		if (null == d)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmss");
		String rets = null;
		try {
			rets = simpleDateFormat.format(d);
		} catch (Exception e) {
		}
		return rets;
	}

	/**
	 * 时间转字符串
	 * 
	 * @param d
	 *            : 时间
	 * @return 成功 转换后串 失败 null
	 */
	public final String MyTimeToString(Date d) {
		if (null == d)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HHmmss");
		String rets = null;
		try {
			rets = simpleDateFormat.format(d);
		} catch (Exception e) {
		}
		return rets;
	}

	/**
	 * 字符串转日期时间
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final Date MyStringToDateTime(String s) {
		if (null == s)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmss");
		Date d = null;
		try {
			d = simpleDateFormat.parse(s);
		} catch (Exception e) {
		}
		return d;
	}

	/**
	 * 字符串转日期
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final Date MyStringToDate(String s) {
		if (null == s)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		Date d = null;
		try {
			d = simpleDateFormat.parse(s);
		} catch (Exception e) {
		}
		return d;
	}

	/**
	 * 字符串转时间
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final Date MyStringToTime(String s) {
		if (null == s)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HHmmss");
		Date d = null;
		try {
			d = simpleDateFormat.parse(s);
		} catch (Exception e) {
		}
		return d;
	}

	/**
	 * 字符串转标准日期字符串
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final String MyStringToISODateString(String s) {
		if (null == s)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		try {
			Date d = simpleDateFormat.parse(s);
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			return simpleDateFormat.format(d);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 字符串转标准日期时间字符串
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final String MyStringToISOTimeString(String s) {
		if (null == s)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HHmmss");
		try {
			Date d = simpleDateFormat.parse(s);
			simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
			return simpleDateFormat.format(d);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 字符串转标准日期时间字符串
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final String MyStringToISODateTimeString(String s) {
		if (null == s)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmss");
		try {
			Date d = simpleDateFormat.parse(s);
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return simpleDateFormat.format(d);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 字符串转标准日期字符串
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final String MyISODateStringToString(String s) {
		if (null == s)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date d = simpleDateFormat.parse(s);
			simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
			return simpleDateFormat.format(d);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 字符串转标准日期字符串
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final String MyDateStringToString(String s) {
		if (null == s)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat();
		String[] patterns = new String[] { "yyyy-MM-dd", "MM/dd/yyyy",
				"yyyy/MM/dd" };
		for (int i = 0; i < patterns.length; i++) {
			sdf.applyPattern(patterns[i]);
			try {
				Date d = sdf.parse(s);
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyyMMdd");
				return simpleDateFormat.format(d);
			} catch (Exception e) {

			}
		}
		return null;
	}

	/**
	 * 标准日期时间字符串转字符串
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final String MyISOTimeStringToString(String s) {
		if (null == s)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
		try {
			Date d = simpleDateFormat.parse(s);
			simpleDateFormat = new SimpleDateFormat("HHmmss");
			return simpleDateFormat.format(d);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 标准日期时间字符串转字符串
	 * 
	 * @param s
	 *            : 日期时间字符串
	 * @return 成功 日期 失败 null
	 */
	public final String MyISODateTimeStringToString(String s) {
		if (null == s)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		try {
			Date d = simpleDateFormat.parse(s);
			simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			return simpleDateFormat.format(d);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 获取当前时间字符串
	 * 
	 * @return 成功 当前时间字符串 失败 null
	 */
	public final String getCurrentDTString() {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyyMMddHHmmss");
			return simpleDateFormat.format(new Date());
		} catch (Exception e) {
		}
		return null;
	}

	/******************** date response end **********************/
	public final String getCurrentDateString() {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
			return simpleDateFormat.format(new Date());
		} catch (Exception e) {
		}
		return null;
	}

	public Integer getHoursBetween(String s_Time, String e_Time) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyyMMddHHmmss");
			Date s_Date = simpleDateFormat.parse(s_Time);
			Date e_Date = simpleDateFormat.parse(e_Time);
			long ms = e_Date.getTime() - s_Date.getTime();
			return (int) ms / 1000 / 60 / 60;
		} catch (Exception e) {
		}
		return null;
	}

	public String getYestodayDateString() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return getDateString(calendar);
	}

	public String getTomorrowDateString() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, +1);
		return getDateString(calendar);
	}

	private String getDateString(Calendar calendar) {
		String ymd = "";
		int y = calendar.get(Calendar.YEAR);
		ymd += y;
		int m = calendar.get(Calendar.MONTH) + 1;
		if (m < 10)
			ymd += "0" + m;
		else
			ymd += m;
		int d = calendar.get(Calendar.DAY_OF_MONTH);
		if (d < 10)
			ymd += "0" + d;
		else
			ymd += d;
		return ymd;
	}

	public final String getLastMonthFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		int m = calendar.get(Calendar.MONTH) + 1;
		int y = calendar.get(Calendar.YEAR);
		int d = 1;
		return m + "/" + d + "/" + y;
	}

	public final String getLastMonthLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		int d = calendar.get(Calendar.DAY_OF_MONTH);
		int m = calendar.get(Calendar.MONTH) + 1;
		int y = calendar.get(Calendar.YEAR);
		return m + "/" + d + "/" + y;
	}

	public final String getCurrentMonthFirstDay() {
		Calendar calendar = Calendar.getInstance();
		int m = calendar.get(Calendar.MONTH) + 1;
		int y = calendar.get(Calendar.YEAR);
		return m + "/" + 1 + "/" + y;
	}

}
